﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using AMG.DALProfile;

namespace AMG.Web.ajax
{
    public partial class settable : System.Web.UI.Page
    {
        string R = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            #region Ajax
            if (Request.Form["num"] !=null)
            {
                int num = int.Parse (Request.Form["num"]);
                try
                {
                    Response.Write(addtr(num));
                }
                catch
                {
                    Response.Write("0");
                }
                Response.End();
            }
            if (Request.Form["isgolf"] != null)
            {
                string isgolf = Request.Form["isgolf"];
                try
                {
                    Response.Write(addgolf(isgolf));
                }
                catch
                {
                    Response.Write("0");
                }
                Response.End();
            }
            if (Request.Form["numedit"] != null)
            {
                int num = int.Parse(Request.Form["numedit"]);
                R = Request.Form["r"];
                try
                {
                    Response.Write(addtredit(num,R));
                }
                catch
                {
                    Response.Write("0");
                }
                Response.End();
            }
            #endregion
        }

        private string addtr(int num)
        {
            string innerhtml="";
            if (num != 0)
            {
                innerhtml = "<table width=\"560\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"15\" class=\"infotab\" >";
                for (int i = 0; i <= num-1; i++)
                {
                    innerhtml += "<tr><td width=\"105\" class=\"center\"><div align=\"right\">品牌和车型</div></td><td width=\"284\" class=\"center\" style=\"padding-left:30px; \"><input name=\"carbland" + (i + 1).ToString() + "\" id=\"carbland" + (i + 1).ToString() + "\" type=\"text\" class=\"tbwidth02\" size=\"15\" width=\"80\" runat=\"server\"  /><input name=\"carname" + (i + 1).ToString() + "\" type=\"text\" class=\"tbwidth02\" size=\"15\" width=\"80\"/></td><td width=\"111\" class=\"center\">&nbsp;</td></tr>";
                }
                innerhtml += "</table>";
            }
            return innerhtml;

        }

        private string addtredit(int num,string R)
        {
            string innerhtml = "";
            if (num != 0)
            {
                DataTable dt = DbHelper.ExecuteTable(CommandType.Text, "select CarBland,CarName from AMG_Cars where UserID='" + R + "'", null);
                innerhtml = "<table width=\"560\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"15\" class=\"infotab\" >";
                if (num >= dt.Rows.Count && dt.Rows .Count !=0)
                {
                    for (int i = 0; i <= dt.Rows.Count - 1; i++)
                    {
                        innerhtml += "<tr><td width=\"105\" class=\"center\"><div align=\"right\">品牌和车型</div></td><td width=\"284\" class=\"center\" style=\"padding-left:30px; \"><input name=\"carbland" + (i + 1).ToString() + "\" type=\"text\" class=\"tbwidth02\" size=\"15\" width=\"80\" value=\"" + dt.Rows[i][0].ToString() + "\"/><input name=\"carname" + (i + 1).ToString() + "\" type=\"text\" class=\"tbwidth02\" size=\"15\" width=\"80\" value=\"" + dt.Rows[i][1].ToString() + "\"/></td><td width=\"111\" class=\"center\">&nbsp;</td></tr>";
                    }
                    for (int i = dt.Rows.Count; i <= num - 1; i++)
                    {
                        innerhtml += "<tr><td width=\"105\" class=\"center\"><div align=\"right\">品牌和车型</div></td><td width=\"284\" class=\"center\" style=\"padding-left:30px; \"><input name=\"carbland" + (i + 1).ToString() + "\" type=\"text\" class=\"tbwidth02\" size=\"15\" width=\"80\"  /><input name=\"carname" + (i + 1).ToString() + "\" type=\"text\" class=\"tbwidth02\" size=\"15\" width=\"80\"/></td><td width=\"111\" class=\"center\">&nbsp;</td></tr>";
                    }
                }
                else if (dt.Rows.Count == 0 && num!=99)
                {
                    for (int i = 0; i <= num - 1; i++)
                    {
                        innerhtml += "<tr><td width=\"105\" class=\"center\"><div align=\"right\">品牌和车型</div></td><td width=\"284\" class=\"center\" style=\"padding-left:30px; \"><input name=\"carbland" + (i + 1).ToString() + "\" type=\"text\" class=\"tbwidth02\" size=\"15\" width=\"80\" value=\"\"/><input name=\"carname" + (i + 1).ToString() + "\" type=\"text\" class=\"tbwidth02\" size=\"15\" width=\"80\" value=\"\"/></td><td width=\"111\" class=\"center\">&nbsp;</td></tr>";
                    }
                }
                else
                {
                    for (int i = 0; i <= num - 1; i++)
                    {
                        innerhtml += "<tr><td width=\"105\" class=\"center\"><div align=\"right\">品牌和车型</div></td><td width=\"284\" class=\"center\" style=\"padding-left:30px; \"><input name=\"carbland" + (i + 1).ToString() + "\" type=\"text\" class=\"tbwidth02\" size=\"15\" width=\"80\" value=\"" + dt.Rows[i][0].ToString() + "\"/><input name=\"carname" + (i + 1).ToString() + "\" type=\"text\" class=\"tbwidth02\" size=\"15\" width=\"80\" value=\"" + dt.Rows[i][1].ToString() + "\"/></td><td width=\"111\" class=\"center\">&nbsp;</td></tr>";
                    }
                    DbHelper.ExecuteNonQuery(CommandType.Text, "DELETE FROM AMG_Cars WHERE  (ID IN (SELECT  TOP " + (dt.Rows.Count - num) + " ID FROM AMG_Cars AS AMG_Cars_1 where UserID='" + R + "' ORDER BY ID DESC))", null);
                    DbHelper.ExecuteNonQuery(CommandType.Text, "update AMG_UserInfo set CarNo=" + num + " where ID=" + R + "", null);
                }
                innerhtml += "</table>";
            }
            return innerhtml;
        }

        private string addgolf(string isgolf)
        {
            string innerhtml = "";
            if (isgolf == "1")
            {
                innerhtml = "<table width=\"560\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"15\" class=\"infotab\"><tr><td width=\"105\" class=\"center\"><div align=\"right\">打球球具</div></td><td width=\"284\" class=\"center\" style=\"padding-left:30px; \"><input name=\"Equipment\" type=\"radio\" value=\"0\" id=\"Equipment1\" runat=\"server\"/>自备<input type=\"radio\" name=\"Equipment\" value=\"1\" id=\"Equipment2\" runat=\"server\"/>租用</td><td class=\"center\" width=\"110\">&nbsp;</td></tr><tr><td width=\"105\" class=\"center\"><div align=\"right\">高尔夫时段*</div></td><td width=\"284\" class=\"center\" style=\"padding-left:30px; \"><select name=\"GolfTime1\" id=\"GolfTime1\" runat=\"server\"><option value=\"1\" selected>9月20日上午</option><option value=\"2\">9月20日下午</option><option value=\"3\">9月21日上午</option><option value=\"4\">9月21日下午</option></select></td><td class=\"center\">&nbsp;</td></tr></table>";
            }
            return innerhtml;
        }
    }
}
